﻿using Common.Prism.Events;
using Common.Prism.Services;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;

namespace Common.Prism.Apps
{
    public class BaseViewModel : BindableBase
    {
        public BaseViewModel()
        {
        }

        #region 短提示

        public void ShowTip(string message)
        {
            Publish(message, TipType.Info);
        }

        public void ShowSuccess(string message)
        {
            Publish(message, TipType.Success);
        }

        public void ShowError(string message)
        {
            Publish(message, TipType.Error);
        }

        public void ShowWarn(string message)
        {
            Publish(message, TipType.Warning);
        }

        #endregion

        #region 强提示

        public void ShowProgressDialog(string title)
        {
            var parameters = new NavigationParameters();
            parameters.Add("DtuId", title);

            var dialog = new DialogData();
            dialog.AllowClose = false;
            dialog.ViewName = "ProgressDialogView";
            dialog.Parameters = parameters;

            ShowDialog(dialog);
        }


        public void ShowConfirmDialog(string title, string content, string confirm = "确定", string cancel = "取消",
            Action<bool> action = null)
        {

            var parameters = new NavigationParameters();
            parameters.Add("DtuId", title);
            parameters.Add("Content", content);
            if (confirm != null)
            {
                parameters.Add("ConfirmText", confirm);
            }
            if (cancel != null)
            {
                parameters.Add("CancelText", cancel);
            }
            if (action != null)
            {
                parameters.Add("Callback", action);
            }

            var dialog = new DialogData();
            dialog.AllowClose = false;
            dialog.ViewName = "ConfirmDialogView";
            dialog.Parameters = parameters;

            ShowDialog(dialog);
        }


        public void ShowDialog(DialogData data)
        {
            Global.EventAggregator.GetEvent<DialogEvent>().Publish(data);
        }


        public void CloseDialog()
        {
            Global.EventAggregator.GetEvent<DialogEvent>().Publish(null);
        }
        #endregion

        public virtual void RequestClose()
        {
            var wps = Global.Container.Resolve<WindowsProviderService>();
            wps.CloseByViewModel(this);
        }


        #region 事件聚合器

        public T GetEvent<T>() where T : EventBase, new()
        {
            return Global.EventAggregator.GetEvent<T>();
        }

        public void Publish(string key)
        {
            var data = new NormalEventData();
            data.Key = key;
            GetEvent<NormalEvent>().Publish(data);
        }

        private void Publish(string message, TipType appearance)
        {
            var data = new ShortTipData();
            data.Message = message;
            data.Appearance = appearance;
            GetEvent<ShortTipEvent>().Publish(data);
        }

        #endregion
    }
}
